package com.example.twotwo_project;

import java.io.BufferedInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomAdapter2 extends ArrayAdapter<Object> {
	
	private ArrayList<LocalData> data;
	View v;
	View v2;
//	ImageView image;
	
	Context context;
	int layout;
	LayoutInflater inflater;
	Integer[] arrayimages;
	

	public CustomAdapter2(Context context, int textViewResourceId,
			ArrayList items) {
		super(context, textViewResourceId, items);
		this.data = items;
	
		this.context = context;
		this.layout = textViewResourceId;
		
		
		inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		arrayimages = new Integer[]{
				R.drawable.heal01,
				R.drawable.heal02,
				R.drawable.heal03,
				R.drawable.heal04,
				R.drawable.heal05,
				R.drawable.heal06,
				R.drawable.heal07,
				R.drawable.heal08
		};
	}
	
	

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return super.getCount();
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return super.getItemId(position);
	}


	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return super.getItem(position);
	}


	@Override
	public int getPosition(Object item) {
		// TODO Auto-generated method stub
		return super.getPosition(item);
	}


	public View getView(int position, View convertView, ViewGroup parent){
		
		//v = convertView;
		v = inflater.inflate(layout, null);
		
		/*생성자메서드로 올림
		if(v == null){
			LayoutInflater vi = (LayoutInflater)getContext().getSystemService(
						Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.serach_itemlist, null);
		}*/
		
		int pos = position%8;
		LinearLayout v_image = (LinearLayout)v.findViewById(R.id.images);
		v_image.setBackgroundResource(arrayimages[pos]);
		
		
		
		
		
		
		LocalData item = data.get(position);
	
		if(item != null){
			
			
			TextView v_title = (TextView)v.findViewById(R.id.title);
			TextView v_phone = (TextView)v.findViewById(R.id.telephone);
			TextView v_address= (TextView)v.findViewById(R.id.roadaddress);
			
			v_title.setText(item.title);
			v_phone.setText("Tel : " +item.telephone);
			v_address.setText("Address : " + item.roadAddress);
			
			
			//리스트에서 보여주기 위한 이미지를 파싱
/*			URL imageUri;
			try{
				imageUri = new URL(item.image);
				HttpURLConnection con = (HttpURLConnection)imageUri.openConnection();
				BufferedInputStream bis = new BufferedInputStream(con.getInputStream(), 10240);
				Bitmap bm = BitmapFactory.decodeStream(bis);
				bis.close();
				image.setImageBitmap(bm);
			}catch(Exception e){
				e.printStackTrace();
			}*/
		}
		return v;
	}

}
